<%-- 
    Document   : catalogues
    Created on : 05.10.2011, 8:00:02
    Author     : Admin
--%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="manufacturerBean.ManufacturerHome"%>
<%@page import="manufacturerBean.Manufacturer"%>
<%@page import="categoryBean.Category"%>
<%@page import="categoryBean.CategoryHome"%>
<%@page import="goodsBean.GoodsHome"%>
<%@page import="goodsBean.Goods"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="DBConnection.ATeamObject"%>
<%@ page errorPage = "Error.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Каталог товаров</title>
        <style type="text/css">
        #head { /* Верхний блок */
        background: #333; /* Цвет фона */
        padding: 5px; /* Поля вокруг текста */
        color: #fff; /* Цвет текста */
        clear: both; /* Отменяем обтекание */
        }
        h1 {
        font-size: 36px; /* Размер шрифта */
        font-weight: bold; /* Жирное начертание */
        margin: 0; /* Убираем отступы */
        color: #fff; /* Цвет текста */
        }
        #foot { /* Нижний блок */
        background: #333; /* Цвет фона */
        padding: 5px; /* Поля вокруг текста */
        color: #fff; /* Цвет текста */
        clear: both; /* Отменяем обтекание */
        }
        </style>
        <%  InitialContext initialContext;
            GoodsHome goodsHome;
            Goods goods;
            Object homeObject;

            CategoryHome categoryHome;
            Category category;
            Object homeObject1;

            Manufacturer manufacturer;
            ManufacturerHome manufacturerHome;
            Object homeObject2;

            ATeamObject ato = new ATeamObject();

            initialContext = new InitialContext();
            homeObject = initialContext.lookup("GoodsBean");
            goodsHome = (GoodsHome) PortableRemoteObject.narrow(homeObject,
                    GoodsHome.class);

            homeObject1 = initialContext.lookup("CategoryBean");
            categoryHome = (CategoryHome) PortableRemoteObject.narrow(homeObject1,
                    CategoryHome.class);

            homeObject2 = initialContext.lookup("ManufacturerBean");
            manufacturerHome = (ManufacturerHome) PortableRemoteObject.narrow(homeObject2,
                    ManufacturerHome.class);
        %>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <div id="head">
        <center><h1>Каталог товаров</h1></center>
        </div>
        <h4>
            <a href="index.jsp">Главная</a> / 
            <a href="catalogues.jsp">Каталог товаров</a>
        </h4>
        <table>
            <tr>
                <td>
                    <form name="MainPageForm" method="post" action="findResults.jsp">
                        <center>
                            <h2>Поиск товаров</h2>
                            <table>
                                <tr>
                                    <td>
                                        <b>Наименование товара</b>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="text" name="GoodsName" size="40">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="SearchingCriteria" value="0" checked="true"> Совпадает
                                    </td>
                                </tr>
                                <tr>
                                    <td> 
                                        <input type="radio" name="SearchingCriteria" value="1"> Содержит
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Цена товара</b>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="text" name="GoodsPriceFrom" size="10">
                                        <input type="text" name="GoodsPriceTo" size="10" id="txt">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="GoodsPriceOptions" value=">" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Больше чем<br>
                                        <input type="radio" name="GoodsPriceOptions" value="<" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Меньше чем<br>
                                        <input type="radio" name="GoodsPriceOptions" value="=" checked="true" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Равно значению<br>
                                        <input type="radio" name="GoodsPriceOptions" value="BETWEEN" onclick="if(this.checked){confirmChoice(this.form,this.value);}"> Лежит в диапазоне<br>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Производитель</b><td>
                                </tr>
                                <%  Collection allManufacturer = manufacturerHome.findAll();
                                    Iterator iter = allManufacturer.iterator();
                                %>
                                <tr>
                                    <td>
                                        <select name="ManufacturerSelect">
                                            <option></option>
                                            <% while (iter.hasNext()) {
                                                   manufacturer = (Manufacturer) iter.next();
                                            %>
                                                   <option value="<%= manufacturer.getObjectId()%>"><%= manufacturer.getName()%></option>
                                            <% }%>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Категория товара</b></td>
                                </tr>
                                <% Collection allCategory = categoryHome.findAll();
                                   iter = allCategory.iterator();
                                %>
                                <tr>
                                    <td>
                                        <select name="GoodsCategorySelect">
                                            <option></option>
                                            <% while (iter.hasNext()) {
                                                   category = (Category) iter.next();

                                            %>
                                                   <option value="<%= category.getObjectId()%>"><%= category.getName()%></option>
                                            <% }%>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="submit" value="Поиск">
                                        <input type="reset" value="Очистить">
                                    </td>
                                </tr> 
                            </table>
                        </center>
                    </form>
                </td>
                <td valign="top">
                    
                    <%  String[] delCategories =
                            request.getParameterValues("delCategories");
                    
                        if ((request.getParameter("history") != null) &&
                                (delCategories != null)) {
                            Collection<Integer> historyCollection =
                                    new ArrayList();
                                   
                            for (String goodsID : delCategories) {
                                    historyCollection.
                                            add(Integer.parseInt(goodsID));
                            }
                            
                            if (request.getSession().
                                    getAttribute("historyKeys") == null) {
                                    request.getSession().setAttribute(
                                            "historyKeys",historyCollection);
                            } else {
                                request.getSession().removeAttribute(
                                        "historyKeys");
                                request.getSession().setAttribute("historyKeys",
                                            historyCollection);
                            }
                            
                            response.sendRedirect("history.jsp?type=1");
                        }    
                    
                        String[] delete = request.getParameterValues("delete");
                        String[] export =
                            request.getParameterValues("export");
                        Collection<Integer> exportKeys = (Collection<Integer>)
                                            request.getSession().getAttribute(
                                            "exportKeys");
                    
                        if ((delCategories != null) && (delete != null)) {
                            for (String catID : delCategories) {
                                category = categoryHome.findByPrimaryKey(
                                        Integer.parseInt(catID));
                                category.remove();
                            }
                        }
                            
                        if ((delCategories != null)&& (export!= null)) {
                            for (String catID : delCategories) {
                                if(!exportKeys.contains(
                                        Integer.parseInt(catID))) {
                                    exportKeys.add(Integer.parseInt(catID));
                                }
                            }           
                        }               

                        allCategory = categoryHome.findAll();
                        iter = allCategory.iterator();
                        if (iter.hasNext()) {
                    %>        
                    
                    <form>
                        <%
                            while (iter.hasNext()) {
                                category = (Category) iter.next();
                        %>
                            <p>
                                <input type="checkbox" name="delCategories"
                                           value="<%=category.getObjectId()%>"/>
                                <a href="goodsList.jsp?catID=<%=category.getObjectId()%>&catName=<%=category.getName()%>">
                                    <%=category.getName()%>
                                </a>
                            </p>
                        <%
                            }
                        %>
                        
                            <button type="submit" name="export" value="true"
                                    formaction="catalogues.jsp"
                                    formmethod="POST" style="width:150px;height:55px">
                                Добавить <br>в экспорт
                            </button>
                        
                        
                            <button type="submit" name="delete" value="true"
                                    formaction="catalogues.jsp"
                                    formmethod="POST" style="width:150px;height:55px">
                                Удалить <br>отмеченные
                            </button>
                        
                        
                            <button type="submit" formaction="addCategory.jsp"
                                    formmethod="POST" style="width:150px;height:55px">
                                Добавить новую<br> категорию
                            </button>
                        
                        
                            <button type="submit" formaction="export.jsp"
                                    formmethod="POST" style="width:150px;height:55px">
                                Перейти на <br>страницу экспорта
                            </button>
                        
                            <button type="submit" name="history" value="true"
                                        formaction="catalogues.jsp"
                                        formmethod="POST" style="width:150px;height:55px">
                                    Посмотреть историю <br> для отмеченных
                            </button>
                    </form>
                    <%
                        } else {
                    %>
                    В базе данных нет ни одной категории товаров!
                    <%
                        }
                    %>
                </td>
            </tr>
        </table>  
        <div id="foot">
        База данных магазина &copy; 2011-2012 
        </div>
    </body>
</html>
